<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">公司管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick"
            >员工管理</span
          >
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex">
          <el-button v-allow="'01020301'" class="btn-light-blue" @click="handleAdd()"
            >添加员工信息</el-button
          >
        </div>
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
        >
          <el-form-item label="">
            <el-cascader
              v-model="searchForm.companyId"
              placeholder="请选择公司名称"
              :options="companyArray"
              :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }"
              @change="changeTreeItem"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-cascader
              v-model="searchForm.departId"
              placeholder="请选择部门名称"
              :options="departArray"
              :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }"
              @change="changeTreeItemDepart"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="searchForm.departPositionId"
              placeholder="请选择职位名称"
              @change="onSubmit"
              clearable
            >
              <el-option
                v-for="item in departPositionArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-input
              maxlength="50"
              show-word-limit
              placeholder="请输入员工姓名"
              v-model="searchForm.realName"
              clearable
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="">
            <el-select
              v-model="searchForm.status"
              placeholder="请选择员工状态"
              @change="onSubmit"
              clearable
            >
              <el-option label="在职" value="0"></el-option>
              <el-option label="离职" value="1"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button v-allow="'01020302'" class="btn-light-blue" @click="onSubmit"
              >查询</el-button
            >
            <el-button v-allow="'01020302'" @click="resetForm">重置</el-button>
          </el-form-item>
          <Import
            v-allow="'01020305'"
            :url="'employeInfo/importEmployeInfo'"
          ></Import>
          <el-button v-allow="'01020306'" @click="handleExport">导出</el-button>
        </el-form>
      </div>
      <NxTable
        :table-cols="tableCols"
        :table-data="tableData"
        :pag-ination="pagination"
        class="table-wrap"
        @refresh="refresh"
      >
        <template slot="headImage" slot-scope="scope">
          <el-image
            style="height: 50px"
            :src="scope.row.headImage"
            :preview-src-list="[scope.row.headImage]"
            fit="contain"
          >
          </el-image>
        </template>
        <template slot="sex" slot-scope="scope">
          <span v-if="scope.row.status == 0">男</span>
          <span v-if="scope.row.status == 1">女</span>
        </template>
        <template slot="status" slot-scope="scope">
          <span class="nx-success" v-if="scope.row.status == 0">在职</span>
          <span class="nx-del" v-if="scope.row.status == 1">离职</span>
        </template>
        <template slot="operation" slot-scope="scope">
          <span
            v-allow="'01020303'"
            @click="handleEdit(scope.row)"
            class="nx-btn nx-edit"
            >编辑</span
          >
          <span
            v-allow="'01020304'"
            @click="handleDelete(scope.row)"
            class="nx-btn nx-del"
            >删除</span
          >
        </template>
      </NxTable>
    </div>
    <AddEditDialog
      :addEditId="addEditId"
      :addEditStatus="addEditStatus"
      :addEditIsDialog="addEditIsDialog"
    ></AddEditDialog>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import AddEditDialog from "./components/addEditDialog.vue";
import Import from "@/components/Import.vue";

import { Page } from "@/utils/pagination";
import {
  getEmployeInfoList,
  delEmployeInfo,
  getCompanyInfoTree,
  getDepartInfoTree,
  getDepartPositions,
  exportEmployeInfo,
} from "@/api/company";

export default {
  components: {
    NxTable,
    AddEditDialog,
    Import,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        companyId: Number(window.localStorage.getItem('companyId')),
        departId: "",
        departPositionId: "",
        realName: "",
        status: "",
      },
      // 表格字段
      tableCols: [
        {
          label: "公司名称",
          prop: "companyName",
          align: "center",
          width: 150,
        },
        {
          label: "部门名称",
          prop: "departName",
          align: "center",
          width: 150,
        },
        {
          label: "职位名称",
          prop: "departPositionName",
          align: "center",
          width: 150,
        },
        {
          label: "头像",
          slot: "headImage",
          align: "center",
          width: 150,
        },
        {
          label: "工号",
          prop: "code",
          align: "center",
          width: 150,
        },
        {
          label: "姓名",
          prop: "realName",
          align: "center",
          width: 150,
        },
        {
          label: "性别",
          slot: "sex",
          align: "center",
          width: 150,
        },
        {
          label: "身份证号",
          prop: "idCard",
          align: "center",
          width: 150,
        },
        {
          label: "出生日期",
          prop: "birthday",
          align: "center",
          width: 150,
        },
        {
          label: "手机号码",
          prop: "phone",
          align: "center",
          width: 150,
        },
        {
          label: "邮箱地址",
          prop: "email",
          align: "center",
          width: 150,
        },
        {
          label: "员工状态",
          slot: "status",
          align: "center",
          width: 150,
        },
        {
          label: "操作",
          slot: "operation",
          align: "center",
          width: 180,
          fixed: "right",
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: "", // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: "add",
      addEditId: "", // 添加编辑rowid
      companyArray: [], // 公司集合
      departArray: [], // 部门集合
      departPositionArray: [], // 职位集合
    };
  },
  // 初始化加载
  mounted() {
    // 查询
    this.getList();

    // 获取公司信息树形列表
    this.getCompanyInfoTree();
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum;
      this.searchForm.pageSize = val.pageSize;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        companyId: "",
        departId: "",
        departPositionId: "",
        realName: "",
        status: "",
      };

      // 清空
      this.departArray = [];
      this.departPositionArray = [];

      this.onSubmit();
    },
    // 查询列表
    getList() {
      getEmployeInfoList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data;
          this.pagination.count = res.data.total;
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delEmployeInfo({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 添加
    handleAdd() {
      this.addEditId = "";
      this.addEditStatus = "add";
      this.addEditIsDialog = true;
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + "";
      this.addEditStatus = "edit";
      this.addEditIsDialog = true;
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false;
      this.typeDialog = false;
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false;
      this.addEditIsDialog = false;
    },
    // 获取公司信息树形列表
    getCompanyInfoTree() {
      getCompanyInfoTree({}).then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
        }
      });
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1];
      } else {
        this.searchForm.companyId = "";
      }

      // 清空
      this.searchForm.departId = "";
      this.departArray = [];

      this.searchForm.departPositionId = "";
      this.departPositionArray = [];

      // 获取部门信息树形列表
      this.getDepartInfoTree();

      // 查询
      this.onSubmit();
    },
    // 获取部门信息树形列表
    getDepartInfoTree() {
      if (!this.searchForm.companyId || this.searchForm.companyId == "") {
        return;
      }
      getDepartInfoTree({ companyId: this.searchForm.companyId }).then(
        (res) => {
          if (res.code == 200) {
            this.departArray = res.data;
          }
        }
      );
    },
    // 部门下拉切换
    changeTreeItemDepart(val) {
      if (val) {
        this.searchForm.departId = val[val.length - 1];
      } else {
        this.searchForm.departId = "";
      }

      // 清空
      this.searchForm.departPositionId = "";
      this.departPositionArray = [];

      // 获取职位信息列表
      this.getDepartPositions();

      // 查询
      this.onSubmit();
    },
    // 获取职位信息列表
    getDepartPositions() {
      if (!this.searchForm.departId || this.searchForm.departId == "") {
        return;
      }
      getDepartPositions({ id: this.searchForm.departId }).then((res) => {
        if (res.code == 200) {
          this.departPositionArray = res.data;
        }
      });
    },
    // 导出
    handleExport() {
      exportEmployeInfo(this.searchForm).then((res) => {
        if (res.code == 200) {
          window.open(res.data);
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>